/// ===========================================================================
/// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
/// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
/// PURPOSE.
/// ===========================================================================
/// 
/// Project:        META Viewer
/// Author:         Leonid Lyublinski 
/// Date:           09/17/2008  Version:        1.0
///
/// ===========================================================================

using System;
using System.Xml;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Data;
using System.Runtime.InteropServices;
using System.Security;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.Office.Server.ApplicationRegistry.Runtime;
using Microsoft.SharePoint.Portal.WebControls;
using SPWebControls = Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages.Communication;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;

using AspWebParts = System.Web.UI.WebControls.WebParts;
using MdModel = Microsoft.Office.Server.ApplicationRegistry.MetadataModel;
using SPWebParts = Microsoft.SharePoint.WebPartPages;


namespace MayaIntl.SharePoint.Portal.METAViewer
{
    public class METARedirect: Page
    {
        const string METAREDIRECT_PAGE = "/ssp/admin/Content/metaviewer.aspx";
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                string itemId;
                string itemUrl;
                string listId;
                string siteUrl;
                string recurrenceId;
                string metaUrl = string.Empty;
                string url = string.Empty;
               
                if (Request.QueryString == null)
                {
                    throw (new ArgumentNullException("SiteUrl", "Invalid reference to the site."));
                }
                else
                {
                    itemId = Request.QueryString["ItemId"]; 
                    if (itemId == "{ItemId}") itemId = string.Empty;
                    
                    itemUrl = Request.QueryString["ItemUrl"];
                    if (itemUrl == "{ItemUrl}") itemUrl = string.Empty;

                    listId = Request.QueryString["ListId"];
                    if (listId == "{ListId}") listId = string.Empty;

                    siteUrl = Request.QueryString["SiteUrl"];
                    if (siteUrl == "{SiteUrl}") siteUrl = string.Empty;
                    
                    recurrenceId = Request.QueryString["RecurrenceId"];
                    if (recurrenceId == "{RecurrenceId}") recurrenceId = string.Empty;                  
                    
                    // build artifact url
                    url = string.Concat(siteUrl, itemUrl);

                    // build META url
                    if (!string.IsNullOrEmpty(itemId) && !string.IsNullOrEmpty(itemUrl))
                    {
                        if (!string.IsNullOrEmpty(listId))
                        {
                            string listUrl = GetListUrl(SPUrlUtility.CombineUrl(siteUrl, itemUrl), listId);
                            metaUrl = GetMetaUrl(listUrl, itemId);
                        
                        }
                        else
                        {
                            // no list
                        }
                    }
                    else
                    {
                        // no item Id, probably page
                        //url = SPUrlUtility.CombineUrl(siteUrl, itemUrl);
                        url = siteUrl;
                    }
                    string redirect = string.Concat(METAREDIRECT_PAGE, "?url=", url);
                    if (!string.IsNullOrEmpty(metaUrl))
                    {
                        redirect += string.Concat("&metaurl=", siteUrl, metaUrl);
                    }
                    SPUtility.Redirect(redirect, SPRedirectFlags.Default, Context);
                }
                
                
                //Response.Redirect(redirect);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        private string GetListUrl(string itemUrl, string listId)
        {
            string url = string.Empty;
            try
            {
                Guid lid = new Guid(listId);                            
                using (SPSite site = new SPSite(itemUrl))
                {
                    SPWeb web = site.OpenWeb();// SPWebControls.SPControl.GetContextWeb(Context);
                    SPList list = web.Lists[lid];
                    url = list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;
                }
            }
            catch { }
            return HttpUtility.UrlDecode(url);
        }
        
        private string GetMetaUrl(string listUrl, string itemId)
        {
            string url = string.Empty;
            int itemIdInt = 0;
            if (int.TryParse(itemId, out itemIdInt) && itemIdInt>0)
                url = HttpUtility.UrlDecode(string.Concat(listUrl, "?ID=", itemId));

            return url;
        }
	}
}
